/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include "gimimessage.h"

namespace gimi {

GIMIMessage::GIMIMessage()
  : arrivalTime (),
    statusMessage (false),
    majorTypeId (0),
    minorTypeId (0),
    messageId (0),
    senderId (0),
    messageFlags (0),
    datalength (0),
    data (NULL)
{

}

GIMIMessage::~GIMIMessage()
{
  if (data != NULL)
    delete[] data;
  data = NULL;
}

int GIMIMessage::getMajorTypeId() const
{
  return this->majorTypeId;
}

int GIMIMessage::getMinorTypeId() const
{
  return this->minorTypeId;
}

int GIMIMessage::getMessageId() const
{
  return this->messageId;
}

int GIMIMessage::getSenderId() const
{
  return this->senderId;
}

int GIMIMessage::getMessageFlags() const
{
  return this->messageFlags;
}

int GIMIMessage::getDatalength() const
{
  return this->datalength;
}

char* GIMIMessage::getData() const
{
  return this->data;
}

bool GIMIMessage::isStatusMessage() const
{
  return this->statusMessage;
}

void GIMIMessage::reset()
{
  statusMessage = false;

  arrivalTime.seconds = 0;
  arrivalTime.useconds = 0;
  
  majorTypeId = 0;
  minorTypeId = 0;
  messageId = 0;
  senderId = 0;
  messageFlags = 0;
  
  if (data != NULL)
    delete[] data;
  
  data = NULL;
  datalength = 0;
}

void GIMIMessage::assign(GIMIMessage &msg)
{
  dPrint(20, "Assigning message %d from 0x%08x, type:%d minortype:%d ",
         msg.messageId, msg.senderId, msg.majorTypeId, msg.minorTypeId);
  reset();

  statusMessage = msg.statusMessage;

  arrivalTime = msg.arrivalTime;
  
  majorTypeId = msg.majorTypeId;
  minorTypeId = msg.minorTypeId;
  messageId = msg.messageId;
  senderId = msg.senderId;
  messageFlags = msg.messageFlags;

  data = msg.data;
  datalength = msg.datalength;

  // Invalidate original.
  msg.data = NULL;
  msg.reset();
}

void GIMIMessage::copy(GIMIMessage &msg)
{
  statusMessage = msg.statusMessage;

  arrivalTime = msg.arrivalTime;
  
  majorTypeId = msg.majorTypeId;
  minorTypeId = msg.minorTypeId;
  messageId = msg.messageId;
  senderId = msg.senderId;
  messageFlags = msg.messageFlags;

  datalength = msg.datalength;
  data = new char[msg.datalength];
  memcpy(data, msg.data, msg.datalength);
}

} // end namespace gimi
